package com.qianwei.footpath.net;

import com.common.baselib.bean.ResultBean;
import com.common.baselib.log.LogUtils;
import com.common.baselib.utils.CommonUtils;
import com.qianwei.footpath.bean.LoginUserBean;
import com.qianwei.footpath.manage.LocalCacheDataManage;
import com.qianwei.footpath.param.Param;
import com.qianwei.footpath.param.WeChatUpdateParam;
import com.qianwei.footpath.utils.Constants;

import org.jetbrains.annotations.NotNull;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class RetrofitUtil {
    private Retrofit mRetrofit;
    private ApiService mApiService;

    private static RetrofitUtil mInstance;
    private final static int maxStale = 7 * 24 * 60;//分钟
    private int maxAge = 6;//分钟

    /**
     * 私有构造方法
     */
    private RetrofitUtil() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //声明缓存地址和大小
        Cache cache = null;
        try {
            cache = new Cache(CommonUtils.getApp().getCacheDir(), 10 * 1024 * 1024);
        } catch (Exception e) {
            LogUtils.e("Could not create http cache", e);
        }
        //日志显示级别
        HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BODY;
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(@NotNull String message) {
                LogUtils.d("OkHttpMessage:" + message);
            }
        });
        loggingInterceptor.setLevel(level);
        builder.cache(cache)
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
//                .addNetworkInterceptor(new XInterceptor.CommonNetCache(maxAge))//设置有网下缓存时间
//                .addInterceptor(new XInterceptor.CommonLog())//日志
                .addInterceptor(new XInterceptor.CommonNoNetCache(maxStale))//设置无网下缓存时间
                .addInterceptor(new XInterceptor.CommonHeader())//添加header头部参数
                .addInterceptor(loggingInterceptor)//打印请求参数和返回信息
                .retryOnConnectionFailure(true);//默认重试一次，若需要重试N次，则要添加XInterceptor.Retry()拦截器。
        mRetrofit = new Retrofit.Builder()
                .client(builder.build())
                .baseUrl(LocalCacheDataManage.getSeverUrl())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        mApiService = mRetrofit.create(ApiService.class);
    }

    public static RetrofitUtil getInstance() {
        if (mInstance == null) {
            synchronized (RetrofitUtil.class) {
                mInstance = new RetrofitUtil();
            }
        }
        return mInstance;
    }

    public void getWeChatUpdate(WeChatUpdateParam param, Subscriber<ResultBean<Boolean>> subscriber) {
        mApiService.getWeChatUpdate(param).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getLoginUser(String imei, Subscriber<ResultBean<LoginUserBean>> subscriber) {
        Param param = new Param();
        param.setImei(imei);
        mApiService.getLoginUser(param).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

}
